package leetcode;

import java.util.ArrayList;

public class Lc130 {

    int[] X = {0, 0, -1, 1};
    int[] Y = {-1, 1, 0, 0};


    boolean canCatch;

    ArrayList<int[]> list = new ArrayList<>();


    void check(char[][] board, boolean[][] visited, int x, int y) {
        if (x == 0 || x == board.length - 1 || y == 0 || y == board[0].length - 1) {
            canCatch = true;
        }
        list.add(new int[]{x, y});
        visited[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int nx = x + X[i];
            int ny = y + Y[i];
            if (nx < 0 || nx >= board.length || ny < 0 || ny >= board[0].length)
                continue;
            if (board[nx][ny] == 'O' && !visited[nx][ny]) {
                if (nx == 0 || nx == board.length - 1 || ny == 0 || ny == board[0].length - 1) {
                    canCatch = true;
                }
                check(board, visited, nx, ny);
            }
        }

    }


    public void solve(char[][] board) {

        boolean[][] visited = new boolean[board.length][board[0].length];

        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == 'O' && !visited[i][j]) {
                    canCatch = false;
                    check(board, visited, i, j);
                    if (!canCatch) {
                        for (int k = 0; k < list.size(); k++) {
                            board[list.get(k)[0]][list.get(k)[1]] = 'X';
                        }
                    }
                    list.clear();
                }
            }
        }
    }


    public void solve2(char[][] board) {




    }









}
